//% color="#5bc2e8" iconWidth=50 iconHeight=40
namespace keras{
    //% block="初始化keras模块" blockType="command"
    export function keras_init(parameter: any, block: any) {
        Generator.addImport(`import tensorflow.keras\nimport numpy as np`)
        Generator.addCode(`
def keras_code(filename):
    test_img=tensorflow.keras.preprocessing.image.load_img(filename,target_size=(224,224,3))
    test_img=tensorflow.keras.preprocessing.image.img_to_array(test_img)
    test_img=test_img/255
    test_img=np.expand_dims(test_img,0)
    pred=model.predict(test_img)
    prediction_class=lables[pred.argmax()]#在lables列表中寻找值
    return prediction_class #返回结果`     
    )}
    //% block="加载keras.h5模型名称:[SS]" blockType="command"
    //% SS.shadow="normal" SS.defl="keras_model.h5"
    export function keras_model(parameter: any, block: any) {
        let X=parameter.SS.code
        Generator.addCode(`model=tensorflow.keras.models.load_model('${X}')`)

    } 

    //% block="lables模型名称:[TT]" blockType="command"
    //% TT.shadow="normal" TT.defl="'石头','剪刀','布'"
    export function keras_lables(parameter: any, block: any) {
        let Y=parameter.TT.code
        Generator.addCode(`lables=[${Y}]`)
    } 

    //% block="载入图片名称:[Img]" blockType="command"
    //% Img.shadow="normal" Img.defl="Mind+.png"
    export function keras_Img(parameter: any, block: any) {
        let Z=parameter.Img.code
        Generator.addCode(`filename=r'${Z}'`)
    } 

    //% block="获取模型对比后的字符串" blockType="reporter"
    export function keras_Valve(parameter: any, block: any) {
        Generator.addCode(`keras_code(filename)`)
    } 

}
